<?php namespace Common\Model;

use \Hdphp\Model\Model;

class Orders extends Model{
	
	protected $table = 'orders';
	
	public function addData(){
		//接收收货地址的said
		$said = Q('post.address',0,'intval');
		//获得订单总价
		$totalprice = Cart::getTotalPrice();
		$shippingaddModel = new \Common\Model\Shippingaddress;
		//获得收货地址的省份
		$province = $shippingaddModel->where("said={$said}")->pluck('province');
		//获得收货的地址的城市
		$city = $shippingaddModel->where("said={$said}")->pluck('city');
		//获得收货地址的区或县
		$county = $shippingaddModel->where("said={$said}")->pluck('county');
		//获得地址
		$address = $shippingaddModel->where("said={$said}")->pluck('address');
		//组合详细地址
		$shippingaddress = $province.$city.$county.$address;
		//取得收货人姓名
		$consignee = $shippingaddModel->where("said={$said}")->pluck('name');
		//生成一个订单号码
		$ordernumber = Cart::getOrderId();
		//获得登陆的时候保存的用户ID
		$user_cusid = $_SESSION['cusid'];
		//组合数组 添加进数据库
		$orderdata = array(
			'generatedtime'=>time(),
			'totalprice'=>$totalprice,
			'shippingaddress'=>$shippingaddress,
			'consignee'=>$consignee,
			'ordernumber'=>$ordernumber,
			'user_cusid'=>$user_cusid,
		);
		//调用add方法 并接收返回的$oid
		$oid = $this->add($orderdata);
		//
		$orderlistModel = new \Common\Model\Orderlist;
		//获得购物订单数据
		$goodsData = cart::getGoods();
		//遍历数组  组合数组 调用add方法添加进数据库
		foreach ($goodsData as $k => $v) {
			$orderlistdata = array(
				'quantity'=>$v['num'],
				'price'=>$v['total'],
				'orders_oid'=>$oid,
				'goods_gid'=>$v['id']
			);
			$orderlistModel->add($orderlistdata);
		}
		return $oid;	
	}
	
} 



 ?>